package se.etsf01.aesp.gui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.*;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import se.etsf01.aesp.ExportProject;
import se.etsf01.aesp.ImportProject;
import se.etsf01.aesp.algo.*;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 *
 * @author Henrik
 */
public class EffortGui extends javax.swing.JFrame {

    private ProjectList projectlist;
    String path;
    private HashMap<Attribute, DefaultComboBoxModel> comboModels;

    /**
     * Creates new form EffortGui
     */
    public EffortGui() {
        fixBoxes();
        initComponents();
        setPosition();
        readFromConfig();
        setVisible(true);

    }

    public void fixBoxes() {
        comboModels = new HashMap<Attribute, DefaultComboBoxModel>();
        for (Attribute attr : Attribute.values()) {
            int min = attr.getMin().ordinal();
            int max = attr.getMax().ordinal();
            Rating[] rating = Rating.values();
            String[] choices = new String[max - min + 1];

            for (int i = min, j = 0; i <= max; i++, j++) {
                choices[j] = rating[i].toString();
            }

            comboModels.put(attr, new DefaultComboBoxModel(choices));
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        LOC = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        SimThreshold = new javax.swing.JSlider();
        jTextField1 = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        RELY = new javax.swing.JComboBox();
        jPanel4 = new javax.swing.JPanel();
        DATA = new javax.swing.JComboBox();
        jPanel5 = new javax.swing.JPanel();
        CPLX = new javax.swing.JComboBox();
        jPanel6 = new javax.swing.JPanel();
        TIME = new javax.swing.JComboBox();
        jPanel7 = new javax.swing.JPanel();
        STOR = new javax.swing.JComboBox();
        jPanel8 = new javax.swing.JPanel();
        VIRT = new javax.swing.JComboBox();
        jPanel9 = new javax.swing.JPanel();
        TURN = new javax.swing.JComboBox();
        jPanel10 = new javax.swing.JPanel();
        ACAP = new javax.swing.JComboBox();
        jPanel11 = new javax.swing.JPanel();
        MODP = new javax.swing.JComboBox();
        jPanel12 = new javax.swing.JPanel();
        AEXP = new javax.swing.JComboBox();
        jPanel13 = new javax.swing.JPanel();
        VEXP = new javax.swing.JComboBox();
        jPanel14 = new javax.swing.JPanel();
        LEXP = new javax.swing.JComboBox();
        jPanel15 = new javax.swing.JPanel();
        PCAP = new javax.swing.JComboBox();
        jPanel16 = new javax.swing.JPanel();
        TOOL = new javax.swing.JComboBox();
        jPanel17 = new javax.swing.JPanel();
        SCED = new javax.swing.JComboBox();
        mnuBar = new javax.swing.JMenuBar();
        mnuDatabase = new javax.swing.JMenu();
        mnuOpenDatabase = new javax.swing.JMenuItem();
        mnuProject = new javax.swing.JMenu();
        mnuExportProject = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("AESP Tool v1");
        setResizable(false);

        jButton1.setText("Ok");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        LOC.setText("100.0");
        LOC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LOCActionPerformed(evt);
            }
        });

        jLabel17.setText("SIZE (kLOC)");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Similarity Threshold"));

        SimThreshold.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                SimThresholdStateChanged(evt);
            }
        });
        SimThreshold.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
                SimThresholdCaretPositionChanged(evt);
            }
        });

        jTextField1.setText("50");
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField1KeyReleased(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(SimThreshold, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 51, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(SimThreshold, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Charachteristics"));
        jPanel2.setLayout(new java.awt.GridLayout(3, 5, 5, 5));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("RELY"));

        RELY.setModel(comboModels.get(Attribute.RELY));
        RELY.setSelectedItem("NOMINAL");

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(RELY, 0, 174, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(RELY, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(0, 21, Short.MAX_VALUE))
        );

        jPanel2.add(jPanel3);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("DATA"));

        DATA.setModel(comboModels.get(Attribute.DATA));
        DATA.setSelectedItem("NOMINAL");

        org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(DATA, 0, 180, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(DATA, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        jPanel2.add(jPanel4);

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("CPLX"));

        CPLX.setModel(comboModels.get(Attribute.CPLX));
        CPLX.setSelectedItem("NOMINAL");

        org.jdesktop.layout.GroupLayout jPanel5Layout = new org.jdesktop.layout.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(CPLX, 0, 180, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(CPLX, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        jPanel2.add(jPanel5);

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("TIME"));

        TIME.setModel(comboModels.get(Attribute.TIME));
        TIME.setSelectedItem("NOMINAL");
        TIME.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TIMEActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel6Layout = new org.jdesktop.layout.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(TIME, 0, 180, Short.MAX_VALUE)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(TIME, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        jPanel2.add(jPanel6);

        jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder("STOR"));

        STOR.setModel(comboModels.get(Attribute.STOR));
        STOR.setSelectedItem("NOMINAL");

        org.jdesktop.layout.GroupLayout jPanel7Layout = new org.jdesktop.layout.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(STOR, 0, 180, Short.MAX_VALUE)
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(STOR, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        jPanel2.add(jPanel7);

        jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder("VIRT"));

        VIRT.setModel(comboModels.get(Attribute.VIRT));
        VIRT.setSelectedItem("NOMINAL");
        VIRT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                VIRTActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel8Layout = new org.jdesktop.layout.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(VIRT, 0, 180, Short.MAX_VALUE)
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(VIRT, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        jPanel2.add(jPanel8);

        jPanel9.setBorder(javax.swing.BorderFactory.createTitledBorder("TURN"));

        TURN.setModel(comboModels.get(Attribute.TURN));
        TURN.setSelectedItem("NOMINAL");

        org.jdesktop.layout.GroupLayout jPanel9Layout = new org.jdesktop.layout.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(TURN, 0, 180, Short.MAX_VALUE)
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(TURN, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        jPanel2.add(jPanel9);

        jPanel10.setBorder(javax.swing.BorderFactory.createTitledBorder("ACAP"));

        ACAP.setModel(comboModels.get(Attribute.ACAP));
        ACAP.setSelectedItem("NOMINAL");

        org.jdesktop.layout.GroupLayout jPanel10Layout = new org.jdesktop.layout.GroupLayout(jPanel10);
        jPanel10.setLayout(jPanel10Layout);
        jPanel10Layout.setHorizontalGroup(
            jPanel10Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(ACAP, 0, 180, Short.MAX_VALUE)
        );
        jPanel10Layout.setVerticalGroup(
            jPanel10Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(ACAP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        jPanel2.add(jPanel10);

        jPanel11.setBorder(javax.swing.BorderFactory.createTitledBorder("MODP"));

        MODP.setModel(comboModels.get(Attribute.MODP));
        MODP.setSelectedItem("NOMINAL");

        org.jdesktop.layout.GroupLayout jPanel11Layout = new org.jdesktop.layout.GroupLayout(jPanel11);
        jPanel11.setLayout(jPanel11Layout);
        jPanel11Layout.setHorizontalGroup(
            jPanel11Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(MODP, 0, 180, Short.MAX_VALUE)
        );
        jPanel11Layout.setVerticalGroup(
            jPanel11Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel11Layout.createSequentialGroup()
                .add(MODP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(0, 21, Short.MAX_VALUE))
        );

        jPanel2.add(jPanel11);

        jPanel12.setBorder(javax.swing.BorderFactory.createTitledBorder("AEXP"));

        AEXP.setModel(comboModels.get(Attribute.AEXP));
        AEXP.setSelectedItem("NOMINAL");

        org.jdesktop.layout.GroupLayout jPanel12Layout = new org.jdesktop.layout.GroupLayout(jPanel12);
        jPanel12.setLayout(jPanel12Layout);
        jPanel12Layout.setHorizontalGroup(
            jPanel12Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(AEXP, 0, 180, Short.MAX_VALUE)
        );
        jPanel12Layout.setVerticalGroup(
            jPanel12Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(AEXP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        jPanel2.add(jPanel12);

        jPanel13.setBorder(javax.swing.BorderFactory.createTitledBorder("VEXP"));

        VEXP.setModel(comboModels.get(Attribute.VEXP));
        VEXP.setSelectedItem("NOMINAL");
        VEXP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                VEXPActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel13Layout = new org.jdesktop.layout.GroupLayout(jPanel13);
        jPanel13.setLayout(jPanel13Layout);
        jPanel13Layout.setHorizontalGroup(
            jPanel13Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(VEXP, 0, 180, Short.MAX_VALUE)
        );
        jPanel13Layout.setVerticalGroup(
            jPanel13Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(VEXP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        jPanel2.add(jPanel13);

        jPanel14.setBorder(javax.swing.BorderFactory.createTitledBorder("LEXP"));

        LEXP.setModel(comboModels.get(Attribute.LEXP));
        LEXP.setSelectedItem("NOMINAL");

        org.jdesktop.layout.GroupLayout jPanel14Layout = new org.jdesktop.layout.GroupLayout(jPanel14);
        jPanel14.setLayout(jPanel14Layout);
        jPanel14Layout.setHorizontalGroup(
            jPanel14Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(LEXP, 0, 180, Short.MAX_VALUE)
        );
        jPanel14Layout.setVerticalGroup(
            jPanel14Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(LEXP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        jPanel2.add(jPanel14);

        jPanel15.setBorder(javax.swing.BorderFactory.createTitledBorder("PCAP"));

        PCAP.setModel(comboModels.get(Attribute.PCAP));
        PCAP.setSelectedItem("NOMINAL");
        PCAP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PCAPActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel15Layout = new org.jdesktop.layout.GroupLayout(jPanel15);
        jPanel15.setLayout(jPanel15Layout);
        jPanel15Layout.setHorizontalGroup(
            jPanel15Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(PCAP, 0, 180, Short.MAX_VALUE)
        );
        jPanel15Layout.setVerticalGroup(
            jPanel15Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel15Layout.createSequentialGroup()
                .add(PCAP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(0, 21, Short.MAX_VALUE))
        );

        jPanel2.add(jPanel15);

        jPanel16.setBorder(javax.swing.BorderFactory.createTitledBorder("TOOL"));

        TOOL.setModel(comboModels.get(Attribute.TOOL));
        TOOL.setSelectedItem("NOMINAL");
        TOOL.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TOOLActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel16Layout = new org.jdesktop.layout.GroupLayout(jPanel16);
        jPanel16.setLayout(jPanel16Layout);
        jPanel16Layout.setHorizontalGroup(
            jPanel16Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(TOOL, 0, 180, Short.MAX_VALUE)
        );
        jPanel16Layout.setVerticalGroup(
            jPanel16Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(TOOL, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        jPanel2.add(jPanel16);

        jPanel17.setBorder(javax.swing.BorderFactory.createTitledBorder("SCED"));

        SCED.setModel(comboModels.get(Attribute.SCED));
        SCED.setSelectedItem("NOMINAL");

        org.jdesktop.layout.GroupLayout jPanel17Layout = new org.jdesktop.layout.GroupLayout(jPanel17);
        jPanel17.setLayout(jPanel17Layout);
        jPanel17Layout.setHorizontalGroup(
            jPanel17Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(SCED, 0, 180, Short.MAX_VALUE)
        );
        jPanel17Layout.setVerticalGroup(
            jPanel17Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(SCED, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        jPanel2.add(jPanel17);

        mnuDatabase.setText("Database");

        mnuOpenDatabase.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        mnuOpenDatabase.setLabel("Open Database...");
        mnuOpenDatabase.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuOpenDatabaseActionPerformed(evt);
            }
        });
        mnuDatabase.add(mnuOpenDatabase);

        mnuBar.add(mnuDatabase);

        mnuProject.setText("Project");

        mnuExportProject.setText("Export Project...");
        mnuExportProject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuExportProjectPerformed(evt);
            }
        });
        mnuProject.add(mnuExportProject);

        jMenuItem1.setText("Import Project...");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuImportProoject(evt);
            }
        });
        mnuProject.add(jMenuItem1);

        mnuBar.add(mnuProject);

        setJMenuBar(mnuBar);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel17)
                            .add(LOC, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 52, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 267, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(jLabel17)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(LOC, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jButton1))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void VEXPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_VEXPActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_VEXPActionPerformed

    private void TOOLActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TOOLActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TOOLActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        try {
            int number = Integer.parseInt(jTextField1.getText());
            if (number < 0) {
                number = 0;
            } else if (number > 100) {
                number = 100;
            }

            SimThreshold.setValue(number);
        } catch (NumberFormatException ex) {
        }
    }//GEN-LAST:event_jTextField1ActionPerformed

    private Project createProject() {
        //To get attributes use the follwing line as a template.
        Project proj = new Project();
        Field[] fields = this.getClass().getDeclaredFields();

        HashMap<String, Attribute> attributes = new HashMap<String, Attribute>();
        for (Attribute attr : Attribute.values()) {
            attributes.put(attr.name(), attr);
        }

        for (Field field : fields) {
            if (attributes.containsKey(field.getName())) {
                try {
                    JComboBox current = (JComboBox) field.get(this);
                    proj.attributes().put(attributes.get(field.getName()), Rating.fromString((String) current.getSelectedItem()));
                } catch (IllegalAccessException ex) {
                    ex.printStackTrace();
                }
            }
        }

        try {
            float linesOfCode = Float.parseFloat(LOC.getText());
            proj.setLinesOfCode(Math.round(linesOfCode * 1000.0f));
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "Invalid Lines of Code value, check that field!", "AESP Tool", JOptionPane.ERROR_MESSAGE);
            return null;
        }

        return proj;
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        Project proj = createProject();

        System.out.println(proj);

        //If you want to run with the database from the config file
        if (projectlist == null) {
            if (path == null) {
                JOptionPane.showMessageDialog(this, "You have to open a database first before doing an estimate.", "Estimation failed", JOptionPane.ERROR_MESSAGE);
                return;
            }
            Parser parser = new Parser(path);
            projectlist = parser.parseFile();
        }

        EstimationFactory factory = new EstimationFactory(projectlist);
        Estimator estim = factory.createEstimator();

        ArrayList<EstimationResult> results = new ArrayList<EstimationResult>();


        try {
            writeToConfig();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this, "IOException trying to write to configfile", "AESP Tool", JOptionPane.ERROR_MESSAGE);
        }
        //TODO: Might not be the fastest way to do it, but it is simple.
        EstimationResult result = estim.estimate(SimThreshold.getValue() / 100.0, proj);
        ReportWindow rp = new ReportWindow(proj, result);


    }//GEN-LAST:event_jButton1ActionPerformed

    private void TIMEActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TIMEActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TIMEActionPerformed

    private void PCAPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PCAPActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_PCAPActionPerformed

    private void VIRTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_VIRTActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_VIRTActionPerformed

    private void mnuOpenDatabaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuOpenDatabaseActionPerformed
        JFileChooser chooser = new JFileChooser();
        chooser.setMultiSelectionEnabled(false);
        chooser.setAcceptAllFileFilterUsed(false);

        FileNameExtensionFilter filter = new FileNameExtensionFilter("Project Database Format files (*.txt)", "txt");
        chooser.setFileFilter(filter);

        chooser.setApproveButtonText("Open");
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            path = chooser.getSelectedFile().getAbsolutePath();
            Parser parser = new Parser(path);
            projectlist = parser.parseFile();
            JOptionPane.showMessageDialog(this, "Found " + String.valueOf(projectlist.size()) + " projects.", "Loading complete.", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_mnuOpenDatabaseActionPerformed

    private void readFromConfig() {
        File config = new File("config.ini");
        if (!config.exists()) {
            System.out.println("cant find config file");

        } else {
            try {
                Scanner scanner = new Scanner(new FileReader(config));
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    if (line.startsWith("path: ")) {
                        path = line.substring(6);

                    } else {
                        System.out.println("no path found on this line");
                    }
                }
            } catch (FileNotFoundException ex) {
                JOptionPane.showMessageDialog(this, "Did not find config file", "AESP Tool", JOptionPane.ERROR_MESSAGE);

            }

        }
    }

    private void writeToConfig() throws IOException {
        File config = new File("config.ini");
        if (!config.exists()) {
            System.out.println("config doesnt exist, creating new one");
        } else {
            config.delete();
        }
        //Creates new config file everytime to make sure the information is correct and up to date.
        if (config.createNewFile()) {
            System.out.println("Created new config file");
        }

        FileWriter fstream = new FileWriter(config);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write("path: " + path);
        out.close();
    }
    private void LOCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LOCActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_LOCActionPerformed

    private void mnuExportProjectPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuExportProjectPerformed
        JFileChooser chooser = new JFileChooser();
        chooser.setMultiSelectionEnabled(false);
        chooser.setAcceptAllFileFilterUsed(false);

        FileNameExtensionFilter filter = new FileNameExtensionFilter("Project files (*.prj)", "prj");
        chooser.setFileFilter(filter);

        chooser.setApproveButtonText("Export");
        if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
            String path = chooser.getSelectedFile().getAbsolutePath();
            ExportProject ep = new ExportProject(path, createProject());
            ep.export();
            JOptionPane.showMessageDialog(this, "Exported project to " + path + ".", "Export complete.", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_mnuExportProjectPerformed

    private void SimThresholdCaretPositionChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_SimThresholdCaretPositionChanged
        jTextField1.setText(String.valueOf(SimThreshold.getValue()));
    }//GEN-LAST:event_SimThresholdCaretPositionChanged

    private void SimThresholdStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_SimThresholdStateChanged
        jTextField1.setText(String.valueOf(SimThreshold.getValue()));
    }//GEN-LAST:event_SimThresholdStateChanged

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
    }//GEN-LAST:event_jTextField1KeyPressed

    private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyTyped
    }//GEN-LAST:event_jTextField1KeyTyped

    private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyReleased
        try {
            int number = Integer.parseInt(jTextField1.getText());
            if (number < 0) {
                number = 0;
            } else if (number > 100) {
                number = 100;
            }

            SimThreshold.setValue(number);
        } catch (NumberFormatException ex) {
        }
    }//GEN-LAST:event_jTextField1KeyReleased

    private void mnuImportProoject(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuImportProoject
        JFileChooser chooser = new JFileChooser();
        chooser.setMultiSelectionEnabled(false);
        chooser.setAcceptAllFileFilterUsed(false);

        FileNameExtensionFilter filter = new FileNameExtensionFilter("Project files (*.prj)", "prj");
        chooser.setFileFilter(filter);
        Project proj = null;
        chooser.setApproveButtonText("Import");
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            String path = chooser.getSelectedFile().getAbsolutePath();
            ImportProject ip = new ImportProject();
            proj = ip.importProj(path);
        }
        Field[] fields = this.getClass().getDeclaredFields();
        HashMap<String, Attribute> attributes = new HashMap<String, Attribute>();
        for (Attribute attr : Attribute.values()) {
            attributes.put(attr.name(), attr);
        }
        for (Field field : fields) {

            if (proj.attributes().containsKey(attributes.get(field.getName()))) {
                try {
                    JComboBox current = (JComboBox) field.get(this);
                    current.setSelectedItem(proj.attributes().get(attributes.get(field.getName())).toString());
                    //proj.attributes().put(attributes.get(field.getName()), Rating.fromString((String) current.getSelectedItem()));
                } catch (IllegalAccessException ex) {
                    ex.printStackTrace();
                }
            }
        }


        int linesOfCode = Math.round(proj.getLinesOfCode() / 1000);
        LOC.setText(Integer.toString(linesOfCode));
        proj.setLinesOfCode(Math.round(linesOfCode * 1000.0f));

    }//GEN-LAST:event_mnuImportProoject

    private void setPosition() {
        // Get the size of the screen
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        // Determine the new location of the window
        int w = getSize().width;
        int h = getSize().height;
        // int x = (dim.width-w)/2;
        // int y = (dim.height-h)/2;

        // Move the window
        setLocation((dim.width - w) / 2, (dim.height - h) / 2);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EffortGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EffortGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EffortGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EffortGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new EffortGui().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox ACAP;
    private javax.swing.JComboBox AEXP;
    private javax.swing.JComboBox CPLX;
    private javax.swing.JComboBox DATA;
    private javax.swing.JComboBox LEXP;
    private javax.swing.JTextField LOC;
    private javax.swing.JComboBox MODP;
    private javax.swing.JComboBox PCAP;
    private javax.swing.JComboBox RELY;
    private javax.swing.JComboBox SCED;
    private javax.swing.JComboBox STOR;
    private javax.swing.JSlider SimThreshold;
    private javax.swing.JComboBox TIME;
    private javax.swing.JComboBox TOOL;
    private javax.swing.JComboBox TURN;
    private javax.swing.JComboBox VEXP;
    private javax.swing.JComboBox VIRT;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel14;
    private javax.swing.JPanel jPanel15;
    private javax.swing.JPanel jPanel16;
    private javax.swing.JPanel jPanel17;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JMenuBar mnuBar;
    private javax.swing.JMenu mnuDatabase;
    private javax.swing.JMenuItem mnuExportProject;
    private javax.swing.JMenuItem mnuOpenDatabase;
    private javax.swing.JMenu mnuProject;
    // End of variables declaration//GEN-END:variables
}
